package com.macro.mall.portal.controller;

import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.ResultCode;
import com.macro.mall.common.exception.ApiException;
import com.macro.mall.common.util.CodeUtil;
import com.macro.mall.model.PmsKeyword;
import com.macro.mall.model.PmsProductCategory;
import com.macro.mall.portal.domain.CombinedPmsProduct;
import com.macro.mall.portal.domain.PmsPortalProductDetail;
import com.macro.mall.portal.domain.PmsProductCategoryNode;
import com.macro.mall.portal.dto.PmsProductDto;
import com.macro.mall.portal.service.PmsKeywordService;
import com.macro.mall.portal.service.PmsPortalProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 前台商品管理Controller
 * Created by macro on 2020/4/6.
 */
@Controller
@Api(tags = "PmsPortalProductController", description = "前台商品管理")
@RequestMapping("/product")
public class PmsPortalProductController {
    Logger logger = LoggerFactory.getLogger(PmsPortalProductController.class);
    @Autowired
    private PmsPortalProductService portalProductService;
    @Autowired
    private PmsKeywordService keywordService;

    @ApiOperation(value = "综合搜索、筛选、排序")
    @ApiImplicitParam(name = "sort", value = "排序字段:0->按相关度；1->按新品；2->按销量；3->价格从低到高；4->价格从高到低",
            defaultValue = "0", allowableValues = "0,1,2,3,4", paramType = "query", dataType = "integer")
    @RequestMapping(value = "/search", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ResponseBody
    public CommonResult<CommonPage<PmsProductDto>> search(@RequestParam(required = false) String keyword,
                                                          @RequestParam(required = false) Integer recommandStatus,
                                                          @RequestParam(required = false) Long productCategoryId,
                                                          @RequestParam Long storeId,
                                                          @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                          @RequestParam(required = false, defaultValue = "5") Integer pageSize,
                                                          @RequestParam(required = false, defaultValue = "0") Integer sort) {

        logger.info("productName-----------------------"+keyword);
        return portalProductService.search(keyword, recommandStatus, productCategoryId, storeId, pageNum, pageSize, sort);
//        return CommonResult.success(CommonPage.restPage(productList));
    }

    @ApiOperation("以树形结构获取所有商品分类")
    @RequestMapping(value = "/categoryTreeList", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmsProductCategory>> categoryTreeList(@RequestParam Long storeId) {
        List<PmsProductCategory> list = portalProductService.categoryTreeList(storeId);
        return CommonResult.success(list);
    }

    @ApiOperation("获取前台商品详情")
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmsPortalProductDetail> detail(@RequestParam Long id) {
        PmsPortalProductDetail productDetail = portalProductService.detail(id,null);
        if (productDetail != null) {
            if (productDetail.getSubStatus() != null && productDetail.getSubStatus().equals(1)) {
                return CommonResult.failed("该组合商品的子商品已被删除或下架");
            }
            return CommonResult.success(productDetail);
        }else {
            return CommonResult.failed("该商品已被删除或下架");
        }
    }

    /**
     * 分页获取组合商品
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/getCombined", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<CombinedPmsProduct>> getCombinedPmsProduct(@RequestParam Long memberId,
                                                                              @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                                              @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        CommonPage<CombinedPmsProduct> combinedPmsProduct = portalProductService.getCombinedPmsProduct(memberId, pageNum, pageSize);
        return CommonResult.success(combinedPmsProduct);
    }

    /**
     * 获取组合商品列表
     * @param memberId
     * @return
     */
    @RequestMapping(value = "/getCombinedRecommend", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<CombinedPmsProduct>> getCombinedPmsProductRecommend(@RequestParam Long memberId) {
        List<CombinedPmsProduct> combinedPmsProduct = portalProductService.getCombinedPmsProductRecommend(memberId);
        return CommonResult.success(combinedPmsProduct);
    }

    /**
     * 获取关键词（热门搜索）
     * 关键词绑定商品id
     * @return
     */
    @RequestMapping(value = "/getKeyword", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult getKeyword(){
        List<PmsKeyword> keyword = keywordService.getKeyword();
        return CommonResult.success(keyword);
    }

}
